/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/


/******************************************************************************/
/* User Global Variable Declaration                                           */
/******************************************************************************/

/* i.e. uint8_t <variable_name>; */

/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include "CoordCom.h"

extern FrameAPI gl_CurrentFrameAPI;

#define BUTTON_SYNCHRONIZED PORTAbits.RA4
#define TIMEOUT_RECEIVE 1

#pragma config WDTEN = OFF      // Watchdog Timer Enable bits (Watch dog timer is always disabled. SWDTEN has no effect.)
#pragma config LVP = OFF

#pragma config STVREN = OFF
#pragma config BOREN = OFF

void main(void)
{
    int iIndex, periodCount = 0;

    TRISAbits.RA4 = 1;

    initLed();
    initCoord();

    eteindreLed(ALL_LED);
    while(1)
    {
        // DEVICES WAKE UP
        allumerLed(IS_NOT_SLEEPING);

        periodCount++;
        iIndex = -1;

        setWatchDog(2*SLEEP_TIME_PERIOD , WD_COORD_SENSOR_APPLI);
        if(gl_RouteTable.curTableIndex != 0)
        {
            if(periodCount == COLLECT_DATA_PERIOD)
            {
                incrementAllErrorCount();
            }

            while(getTimeLeft(WD_COORD_SENSOR_APPLI) > (unsigned)SLEEP_TIME_PERIOD)
            {
                iIndex = requestDataFromFirstNonResponding();
                if(recept(TIMEOUT_RECEIVE) != 0)
                {
                    switch(gl_CurrentFrameAPI.content.frame.frameType)
                    {
                        case API_CMD_RECV_PACKET:
                            handleDataPacket();
                            break;
                        default:
                            break;
                    }
                }
                else if (iIndex >= 0)
                {
                    allumerLed(DATA_RECEIVED);
                    incrementErrorCount(iIndex);
                    delaySec(1);
                }
            }
            eteindreLed(IS_NOT_SLEEPING);
        }
        while(getTimeLeft(WD_COORD_SENSOR_APPLI) > (unsigned)0)
        {
            if(recept(1) != 0)
            {
                switch(gl_CurrentFrameAPI.content.frame.frameType)
                {
                    case API_CMD_RECV_PACKET:
                        handleDataPacket();
                        break;
                    default:
                        break;
                }
            }
        }
        if(periodCount == COLLECT_DATA_PERIOD)
        {
            periodCount = 0;
        }
    }
}